package com.store.app.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.store.app.BaseActivity;
import com.store.app.R;
import com.store.app.adapter.HPTOpenDetailAdapter;
import com.store.app.bean.HPTOpenDetailBean;
import com.store.app.bean.HPTOpenDetailPeopleBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.IDUtil;
import com.store.app.widget.MyListView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * author lushan
 * on 2017/3/24 0024 14:29
 * 火拼团开团详情(新)
 */

public class HPTOpenDetailActivity extends BaseActivity implements IOnHttpListener {
    private TextView txt_order_number;//订单编号
    private TextView txt_statue;//订单状态
    private TextView t_good_name;//产品名称
    private ImageView img_good;//产品图片
    private TextView txt_chengtuan_price;//成团金额
    private TextView txt_open_time;//开团时间
    private TextView txt_end_time;//截止时间
    private TextView txt_open_people;//开团人
    private TextView txt_pe_num;//开团人数
    private MyListView myLV;//订单信息
    private HPTOpenDetailAdapter adapter;
    private List<HPTOpenDetailPeopleBean.ListBean> mList = new ArrayList<>();

    private TextView title;
    private NetRequest netRequest;
    private String s_activity_id;
    private ScrollView mScrollView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hpt_open_detail);

        initView();
        initdata();
        initListener();
    }

    private void initdata() {

            netRequest.requestHtpDetails(1, s_activity_id);//获取详情
             netRequest.getJsonPerson(2, s_activity_id);//参加人
    }

    private void initView(){
        netRequest = new NetRequest(this, this);
        title = (TextView) findViewById(R.id.tvTitle);
        title.setText("详情");
        s_activity_id = getIntent().getStringExtra("activity_id");

        txt_order_number = (TextView)findViewById(R.id.txt_order_number);//订单编号
        txt_statue = (TextView)findViewById(R.id.txt_statue);//订单状态
        t_good_name = (TextView)findViewById(R.id.t_good_name);//产品名称
        img_good = (ImageView) findViewById(R.id.img_good);//产品图片
        txt_chengtuan_price = (TextView)findViewById(R.id.txt_chengtuan_price);//成团金额
        txt_open_time = (TextView)findViewById(R.id.txt_open_time);//开团时间
        txt_end_time = (TextView)findViewById(R.id.txt_end_time);//截止时间
        txt_open_people = (TextView)findViewById(R.id.txt_open_people);//开团人
        txt_pe_num = (TextView)findViewById(R.id.txt_pe_num);//开团人数
        myLV = (MyListView)findViewById(R.id.myLV);//订单信息

        adapter = new HPTOpenDetailAdapter(this,mList);
        myLV.setAdapter(adapter);

        mScrollView = (ScrollView)findViewById(R.id.mScrollView);
        mScrollView.smoothScrollTo(0,20);
    }

    private void initListener(){
        findViewById(R.id.public_ll_return).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
//        netRequest.requestHtpDetails(1, s_activity_id);//获取详情
    }

    @Override
    public void onAfinalFail() {

    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        if(requestId == 1){//详情
            HPTOpenDetailBean bean = new Gson().fromJson(result,HPTOpenDetailBean.class);
            if(bean!=null){
                txt_order_number.setText("活动编号  "+bean.getDetail().getActivity_no());
                txt_statue.setText(IDUtil.hptActivityStatus(bean.getDetail().getStatus()));
                t_good_name.setText(bean.getDetail().getTitle());
                String picinfo = bean.getDetail().getGoods_json_data();
               HashMap<String, String> urlMap = bean.getDoc_url();
                if (!TextUtils.isEmpty(picinfo)) {
                    try {
                        JSONObject jsonObject = new JSONObject(picinfo);
                        String img = jsonObject.getString("path_id");
                        img = urlMap.get(img);
                        ImageLoader.getInstance().displayImage(img, img_good, options);
                    } catch (Exception e) {

                    }
                }
                txt_chengtuan_price.setText("¥" +bean.getDetail().getLadder_price());
                txt_open_time.setText(bean.getDetail().getCreated_date());
                txt_open_people.setText(bean.getDetail().getMember_info());
                txt_end_time.setText(bean.getDetail().getExpiried_date());
                txt_pe_num.setText(bean.getDetail().getMin_num());
            }
        }else if (requestId == 2){//参加人
            HPTOpenDetailPeopleBean bean = new Gson().fromJson(result,HPTOpenDetailPeopleBean.class);
            mList.addAll(bean.getList());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {

    }
    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .showImageOnLoading(R.drawable.default_picture_s)
            .showImageOnFail(R.drawable.default_picture_s)
            .showStubImage(R.drawable.default_picture_s)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
}
